<template>
  <div class="app-container">
    <el-form
      ref="ruleForm"
      :model="ruleForm"
      :rules="rules"
      label-width="150px"
      class="demo-ruleForm"
      style="width: 800px"
    >
      <el-form-item label="学生姓名" prop="studentName">
        <el-input v-model="ruleForm.studentName" />
      </el-form-item>
      <el-form-item label="照片" prop="photo">
        <my-upload v-model="ruleForm.photo" :limit="1" />
      </el-form-item>
      <el-form-item label="学籍号" prop="studentNumber">
        <el-input v-model="ruleForm.studentNumber" />
      </el-form-item>
      <el-form-item label="身份证号" prop="idCardNumber">
        <el-input v-model="ruleForm.idCardNumber" />
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-radio-group v-model="ruleForm.sex">
          <el-radio :label="1">男</el-radio>
          <el-radio :label="2">女</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="民族" prop="nation">
        <el-input v-model="ruleForm.nation" />
      </el-form-item>
      <el-form-item label="联系电话" prop="mobile">
        <el-input v-model="ruleForm.mobile" />
      </el-form-item>
      <el-form-item label="出生日期" prop="birthday">
        <el-date-picker
          v-model="ruleForm.birthday"
          type="date"
          placeholder="请选择出生日期"
        />
      </el-form-item>
      <el-form-item label="所属乡镇" prop="areaId">
        <el-radio-group v-model="ruleForm.areaId">
          <el-radio v-for="item in villageOptions" :key="item.value" :label="item.value">{{ item.label }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="班级" required>
        <el-col :span="7">
          <el-form-item prop="grade">
            <el-select v-model="ruleForm.grade" placeholder="请选择年级">
              <el-option v-for="item in gradeOptions" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col class="line" :span="2" style="width: 20px; height: 20px;" />
        <el-col :span="7">
          <el-form-item prop="classes">
            <el-select v-model="ruleForm.classes" placeholder="请选择班级">
              <el-option v-for="item in classOptions" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-form-item>
      <el-form-item label="政治面貌" prop="politicalStatus">
        <el-select
          v-model="ruleForm.politicalStatus"
          placeholder="请选择政治面貌"
        >
          <el-option v-for="item in politicalStatusOptions" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="户口性质" prop="householdType">
        <el-select
          v-model="ruleForm.householdType"
          placeholder="请选择户口性质"
        >
          <el-option label="农业户口" :value="1" />
          <el-option label="非农业户口" :value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="入学年月" prop="enterSchoolTime">
        <el-date-picker
          v-model="ruleForm.enterSchoolTime"
          type="month"
          placeholder="请选择入学年月"
        />
      </el-form-item>
      <el-form-item label="家庭住址" prop="homeAddress">
        <el-input v-model="ruleForm.homeAddress" />
      </el-form-item>
      <el-form-item label="班内学号" prop="insideClassNo">
        <el-input v-model="ruleForm.insideClassNo" />
      </el-form-item>
      <el-form-item label="学生来源" prop="studentSource">
        <el-input v-model="ruleForm.studentSource" />
      </el-form-item>
      <el-form-item label="残疾证号" prop="disabilityCardNo">
        <el-input v-model="ruleForm.disabilityCardNo" />
      </el-form-item>
      <el-form-item label="残疾类型" prop="disabilityType">
        <el-select
          v-model="ruleForm.disabilityType"
          placeholder="请选择残疾类型"
        >
          <el-option v-for="item in disabilityTypeOptions" :key="item.value" :label="item.label" :value="item.value">{{ item.label }}</el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="残疾等级" prop="disabilityLevel">
        <el-input v-model="ruleForm.disabilityLevel" />
      </el-form-item>
      <el-form-item label="家庭成员姓名" prop="familyName">
        <el-input v-model="ruleForm.familyName" />
      </el-form-item>
      <el-form-item label="家庭成员关系" prop="familyRelation">
        <el-input v-model="ruleForm.familyRelation" />
      </el-form-item>
      <el-form-item label="家庭成员联系电话" prop="familyMobile">
        <el-input v-model="ruleForm.familyMobile" />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="ruleForm.remark" type="textarea" />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          @click="submitForm('ruleForm')"
        >保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import moment from 'moment'
import MyUpload from '@/components/my-upload/index'
import { getStudentDetail, addStudent, getAreaList } from '@/api/basic'
import { disabilityTypeOptions, politicalStatusOptions, gradeOptions, classOptions } from '../../attr'
export default {
  components: { MyUpload },
  data() {
    return {
      disabilityTypeOptions,
      politicalStatusOptions,
      gradeOptions,
      classOptions,
      villageOptions: [],
      ruleForm: {},
      rules: {
        studentName: [
          { required: true, message: '请输入学生姓名', trigger: 'blur' },
          { max: 20, message: '学生姓名不超过20个字符', trigger: 'blur' }
        ],
        studentNumber: [
          { required: true, message: '请输入学生学籍号', trigger: 'blur' }
        ],
        idCardNumber: [
          { required: true, message: '请输入学生身份证号', trigger: 'blur' },
          {
            max: 18,
            min: 18,
            message: '请输入正确的18位身份证号',
            trigger: 'blur'
          }
        ],
        mobile: [
          { required: true, message: '请输入联系电话', trigger: 'blur' }
        ],
        sex: [{ required: true, message: '请选择学生性别', trigger: 'change' }],
        areaId: [{ required: true, message: '请选择所属乡镇', trigger: 'change' }],
        disabilityCardNo: [
          { required: true, message: '请输入残疾证号', trigger: 'blur' }
        ],
        disabilityType: [{ required: true, message: '请选择残疾类型', trigger: 'change' }],
        disabilityLevel: [{ required: true, message: '请选择残疾等级', trigger: 'change' }],
        nation: [
          { required: true, message: '请选择学生民族', trigger: 'change' }
        ],
        birthday: [
          { required: true, message: '请选择出生日期', trigger: 'change' }
        ],
        grade: [{ required: true, message: '请选择年级', trigger: 'change' }],
        classes: [{ required: true, message: '请选择班级', trigger: 'change' }],
        insideClassNo: [
          { required: true, message: '请输入班内学号', trigger: 'blur' }
        ],
        studentSource: [
          { required: true, message: '请输入学生来源', trigger: 'blur' }
        ],
        politicalStatus: [
          { required: true, message: '请选择政治面貌', trigger: 'change' }
        ],
        health: [
          { required: true, message: '请选择健康状况', trigger: 'change' }
        ],
        householdType: [
          { required: true, message: '请选择户口性质', trigger: 'change' }
        ],
        enterSchoolTime: [
          { required: true, message: '请选择入学年月', trigger: 'change' }
        ],
        homeAddress: [
          { required: true, message: '请输入家庭住址', trigger: 'blur' }
        ],
        familyName: [
          { required: true, message: '请输入家长姓名', trigger: 'blur' },
          { max: 20, message: '家长姓名不超过20个字符', trigger: 'blur' }
        ],
        familyRelation: [
          { required: true, message: '请输入家庭成员关系', trigger: 'blur' }
        ],
        familyMobile: [
          {
            required: true,
            message: '请输入正确的家庭成员联系电话',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  mounted() {
    console.log(this.$route)
    const route = this.$route
    if (route.name === 'student-edit') {
      this.getDetail(route.params.id)
    }
    this.getArea()
  },
  methods: {
    async getDetail(id) {
      try {
        const res = await getStudentDetail({
          id: Number(id)
        })
        if (res) {
          const result = JSON.parse(JSON.stringify(res))
          result.grade = res.grade ? Number(res.grade) : null
          result.classes = res.classes ? Number(res.classes) : null
          if (res.photo) {
            result.photo = [{
              url: res.photo
            }]
          } else {
            result.photo = []
          }
          this.ruleForm = result
        }
      } catch (err) {
        console.log('接口出错')
      }
    },
    submitForm(formName) {
      const that = this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const params = JSON.parse(JSON.stringify(that.ruleForm))
          params.birthday = moment(that.ruleForm.birthday).format('YYYY-MM-DD')
          params.enterSchoolTime = moment(that.ruleForm.enterSchoolTime).format(
            'YYYY-MM'
          )
          if (that.ruleForm.photo) {
            params.photo = that.ruleForm.photo[0].url
          }
          addStudent(params).then((res) => {
            if (res) {
              that.$message({
                message: `${this.$route.name === 'student-edit' ? '编辑' : '新增'}学生成功！`,
                type: 'success'
              })
              that.$router.push('/basic/student')
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    async getArea() {
      try {
        const res = await getAreaList({})
        if (res) {
          this.villageOptions = res.map(v => ({
            label: v.areaName,
            value: v.id
          }))
        }
      } catch (err) {
        console.log(err)
      }
    }
  }
}
</script>
<style scoped lang="less">
ul,
li {
  padding: 0;
  margin: 0;
}
li {
  list-style: none;
}
.app-container {
  background: #fff;
  flex: 1;
}
.match-type-check-area {
  margin-top: 12px;
  .left {
    display: block;
    margin-bottom: 5px;
  }
}
</style>
